package com.java.actual.snake;

import java.awt.Color;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;

import javax.swing.JOptionPane;

public class Snake {

	//由Cell组成的蛇的集合
	private LinkedList<Cell> snake = new LinkedList<Cell>();
	//食物的Cell
	private Cell foodCell;
	//食物颜色
	private Color foodColor = Color.GREEN;
	//蛇颜色
	private Color snakeColor = Color.BLACK;
	//移动速度
	private int speed = 500;
	
	private SnakeFrame snakeFrame;
	
	public static final int LEFT = 0;
	public static final int RIGTH = 1;
	public static final int UP = 2;
	public static final int DOWN = 3;

	//方向
	public int flag = 1;
	//得分
	public int success = 0;
	
	public Snake(SnakeFrame snakeFrame){
		this.snakeFrame = snakeFrame;
		//随机生成一个食物
		foodCell = new Cell(new Random().nextInt(StagePanel.COL_NUM),
				new Random().nextInt(StagePanel.ROW_NUM),10,foodColor);
		//初始化蛇
		for(int i = 4; i >= 0; i--){
			snake.add(new Cell(i,0,10,snakeColor));
		}
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}
	
	public Cell getFoodCell() {
		return foodCell;
	}

	public void setFoodCell(Cell foodCell) {
		this.foodCell = foodCell;
	}

	public int getSuccess() {
		return success;
	}

	public void setSuccess(int success) {
		this.success = success;
	}

	public LinkedList<Cell> getSnake() {
		return snake;
	}

	public void setSnake(LinkedList<Cell> snake) {
		this.snake = snake;
	}

	public Snake(LinkedList<Cell> snake) {
		super();
		this.snake = snake;
	}
	
	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public Color getFoodColor() {
		return foodColor;
	}

	public void setFoodColor(Color foodColor) {
		this.foodColor = foodColor;
	}

	public Color getSnakeColor() {
		return snakeColor;
	}

	public void setSnakeColor(Color snakeColor) {
		this.snakeColor = snakeColor;
	}
	
	//蛇移动
	public boolean move(){
		//得到蛇尾
		Cell lastCell = snake.getLast();
		//得到蛇头
		Cell firstCell = snake.getFirst();
		//判断是否死了
		if (isDead(firstCell)){
			return false;
		}
		//吃食物
		eat();
		//向右移动
		if (flag == RIGTH){
			lastCell.setX(firstCell.getX() + 1);
			lastCell.setY(firstCell.getY());
			snake.addFirst(lastCell);
			snake.removeLast();
		}
		//向下移动
		if (flag == DOWN){
			lastCell.setX(firstCell.getX());
			lastCell.setY(firstCell.getY() + 1);
			snake.addFirst(lastCell);
			snake.removeLast();
		}
		//向左移动 
		if (flag == LEFT){
			lastCell.setX(firstCell.getX() - 1);
			lastCell.setY(firstCell.getY());
			snake.addFirst(lastCell);
			snake.removeLast();
		}
		//向上移动
		if (flag == UP){
			lastCell.setX(firstCell.getX());
			lastCell.setY(firstCell.getY() - 1);
			snake.addFirst(lastCell);
			snake.removeLast();
		}
		
		return true;
	}
	
	//判断是否死了
	public boolean isDead(Cell firstCell){
		//判断是否咬到自己
		Iterator<Cell> it = snake.iterator();
		int i = 0;
		while(it.hasNext()){
			if (i++ == 0){
				it.next();
				continue;
			}
			Cell itCell = it.next();
			if(firstCell.getX() == itCell.getX()
					&& firstCell.getY() == itCell.getY()){
				JOptionPane.showMessageDialog(snakeFrame, "Game Over！最终得分：" + success);
				return true;
			}
		}
		//判断是否越左界
		if (firstCell.getX()*StagePanel.GAP < 0){
			JOptionPane.showMessageDialog(snakeFrame, "Game Over！最终得分：" + success);
			return true;
		}
		//判断是否越右界
		if (firstCell.getX()*StagePanel.GAP > StagePanel.COL_NUM*StagePanel.GAP+StagePanel.GAP){
			JOptionPane.showMessageDialog(snakeFrame, "Game Over！最终得分：" + success);
			return true;
		}
		//判断是否越上界
		if (firstCell.getY()*StagePanel.GAP < 0){
			JOptionPane.showMessageDialog(snakeFrame, "Game Over！最终得分：" + success);
			return true;
		}
		//判断是否越下界
		if (firstCell.getY()*StagePanel.GAP > StagePanel.ROW_NUM*StagePanel.GAP+StagePanel.GAP+StagePanel.GAP){
			JOptionPane.showMessageDialog(snakeFrame, "Game Over！最终得分：" + success);
			return true;
		}
		return false;
	}
	
	//吃食物
	public void eat(){
		Cell firstCell = snake.getFirst();
		if (firstCell.getX() == foodCell.getX()
				&& firstCell.getY() == foodCell.getY()){
			snake.addFirst(foodCell);
			this.foodCell = new Cell(new Random().nextInt(StagePanel.COL_NUM),
					new Random().nextInt(StagePanel.ROW_NUM),10,foodColor);
			success += 10;
		}
	}
}
